


## To illustrate selection into marginality: Beta distribution 

the.by = .01 
thetas = seq(the.by/2, 1 - the.by/2, the.by)
alph = 2
bet = 5
unnorm.prior = dbeta(thetas, shape1 = alph, shape2 = bet)
median.prior = qbeta(.5, shape1 = alph, shape2 = bet)
mean.prior = alph/(alph + bet) # prior mean comes from analytical solution


pdf("posterior_and_prior_for_paper.pdf", width = 5.5, height = 4) 
par(mar = c(2,1,1,1))
plot(thetas, unnorm.prior, type = "l", ylim = c(0,4.5), xlim = c(-.055, max(thetas)), axes = F, ylab = "", xlab = "", lwd = 2) 
mtext(expression(theta), side = 1, line = 1)
axis(1, labels = c("test", "test"), at = c(-1, 100))
unnorm.posterior = dbeta(thetas, shape1 = alph, shape2 = bet)*dbeta(thetas, shape1 = alph, shape2 = bet)
norm.posterior = unnorm.posterior*(sum(unnorm.prior)/sum(unnorm.posterior))
lines(thetas, norm.posterior, lwd = 2, lty = 2)
diff = .35
text(.6, .8 + diff, labels = "Distribution of\ncandidates:\n", cex = .8) 
text(.6, .8, labels = expression(italic(g(theta)))) 
text(.1, 3.25 + diff, labels = "Distribution of\nmarginal winners and losers:\n", cex = .8) 
text(.1, 3.25, labels = expression(italic(g^2*(theta)))) 
dev.off()




## Binary types example

p.winner = function(q, n = 1){
	# quality of someone who won n times
	(q*(1 - q/2)^n)/(q*(1 - q/2)^n + (1-q)*((1 - q)/2)^n)
}

quality.of.marginal.winner.n.terms = function(q, n){
	q*p.winner(q,n)/(q*p.winner(q,n) + (1 - q)*(1 - p.winner(q,n)))
}

fn = "binary_types_3_lines_b_for_paper.pdf"
pdf(fn, width = 4.5, height = 4.5) # family = "CM Roman")
par(mfrow = c(1,1), mar = c(5,4,1,1))
qs = seq(0, 1, length = 101)
plot(qs, quality.of.marginal.winner.n.terms(qs,0), type = "l", lwd = 2, xlab = "Quality of candidate pool (q)", ylab = "Quality of candidates")
lines(qs, quality.of.marginal.winner.n.terms(qs,1), type = "l", lwd = 2, lty = 2)
lines(qs, p.winner(qs,1), type = "l", lwd = 2, lty = 3)
abline(a = 0, b = 1, lty = 1, col = rgb(.1,.1,.1, alpha = .5)) 
tcex = .7
text(.58, .2, labels = "Marginal winners and losers\nof open-seat races", cex = tcex)
text(.2, .6, labels = "Winners of\nopen-seat race", cex = tcex)
text(.44, .95, labels = "Marginal winners and losers of\n races involving winners of\nopen-seat races", cex = tcex)
dev.off()







##### Table indicating rates of re-running in US elections ### #

path_to_data = "./../data/scareoff_files/"  # this is the path to the Hall and Snyder replication data, which can be found at http://dx.doi.org/10.7910/DVN/27279

m = matrix(NA, nrow = 3, ncol = 4)
colnames(m) = c("Winner runs, loser doesn't", "Loser runs, winner doesn't", "Both run", "Neither run")
rownames(m) = c("US House, 1948-2012", "US State Legislatures, 1968-2010", "US Statewide Offices, 1970-2012")

d = read.table(paste0(path_to_data, "tmp_rdd_us_house_period_2.tab"), header = T, sep = "\t")
use = abs(d$rv) < 5 
tab = table(d$next_same_L[use], d$next_same_W[use])
m[1, ] = c(tab[1,2], tab[2,1], tab[2,2], tab[1,1])

d = read.table(paste0(path_to_data, "tmp_rdd_stleg.tab"), header = T, sep = "\t")
use = abs(d$rv) < 5 
tab = table(d$next_same_L[use], d$next_same_W[use])
m[2, ] = c(tab[1,2], tab[2,1], tab[2,2], tab[1,1])

use.tl = abs(d$rv) < 5 & ((d$state == "MI" & d$year > 1998) | (d$state == "CA" & d$year > 1996) | (d$state == "AR" & d$year > 1998))
tab.tl = table(d$next_same_L[use.tl], d$next_same_W[use.tl])

d = read.table(paste0(path_to_data, "tmp_rdd_us_statewide.tab"), header = T, sep = "\t")
use = abs(d$rv) < 5
tab = table(d$next_same_L[use], d$next_same_W[use]) 
m[3, ] = c(tab[1,2], tab[2,1], tab[2,2], tab[1,1])

m.props = m/apply(m, 1, sum)
# open seat indicators also in here for each case. 

library(xtable)
write(print(xtable(m.props, digits = 2)), file = "running_again.tex")





##### The asymmetric case: for appendix


quad.formula.expectations = function(A = null, a = 6, b = 6*A - 6, c = 2*A^2 - 3*A + 1){ # very slightly different, but it makes sense
	disc = b^2 - 4*a*c
	(-b + c(-1, 1)*sqrt(disc))/(2*a)
}

As = seq(-1, 1, .01)
solns = matrix(NA, nrow = 0, ncol = 2)
for(A in As){
	qf = quad.formula.expectations(A = A)
	solns = rbind(solns, qf)
}

mat = cbind(As, solns, NA, NA)
mat[,4:5] = mat[,2:3] + mat[,1]
colnames(mat) = c("diff", "p.sol.1", "p.sol.2", "q.sol.1", "q.sol.2")

# library(extrafont)
# Sys.setenv("R_GSCMD" = "/usr/local/bin/gs")
fn = "three_type_solutions_expectations_v2_for_paper.pdf"
pdf(fn, width = 5, height = 5) #  family = "CM Roman")
par(mfrow = c(1,1), mar = c(5, 4, 1, 1))
plot(0:1, 0:1, type = "n", xlab = "p (probability of low type)", ylab = "q (probability of high type)", axes = F)
axis(1);axis(2)
gray = rgb(.1,.1,.1, alpha = .5)
blue = rgb(.1,.1,1, alpha = .5)
# lines(mat[,2], mat[,4], col = gray, lwd = 2)
# lines(mat[,3], mat[,5], col = gray, lwd = 2)
legit.1 = (mat[,2] + mat[,4]) >= 0 & (mat[,2] + mat[,4] <= 1) & mat[,2] >= 0 & mat[,2] <= 1 & mat[,4] >= 0 & mat[,4] <= 1  # this is the key group
lines(mat[legit.1[-c(1, length(legit.1))], c(2,4)], col = blue, type = "l", lwd = 3)
#legit.2 = (mat[,3] + mat[,5]) >= 0 & (mat[,3] + mat[,5] <= 1) & mat[,3] >= 0 & mat[,3] <= 1 & mat[,5] >= 0 & mat[,5] <= 1
lines(x = c(0,1/2), y = c(0,1/2), col = blue, lwd = 3)
abline(a = 1, b = -1, lty = 3)
abline(h = 0, lty = 3)
abline(v = 0, lty = 3)
text.cex = .6
text(.23, .05, "Incumbency\nadvantage", cex = text.cex)
text(.55, .2, "Incumbency\ndisadvantage", cex = text.cex)
text(.1, .23, "Incumbency\ndisadvantage", cex = text.cex)
text(.22, .5, "Incumbency\nadvantage", cex = text.cex)
dev.off()
# embed_fonts(fn)


